﻿@using System.Text
@using NetSurveillance.WebApp.Utilities
@model NetSurveillance.WebApp.Models.ServerStatusModel

@{
    ViewBag.Title = Resources.MenuResource.TopMenu_SystemStatus;
}
@section javascript
{
    <script type="text/javascript">
        $(function() {
            getServerStatus();
            $('#btnViewTask').click(function() {
                $.showLoading('请等待');
                $(this).attr('disabled', true);
                window.location.href = '@Url.Action("TaskDetail","Task")';
            });
            @Html.Partial("_ShowErrorMessage")
            if (top.location != self.location)
                top.location.href = '@Url.Action("Index","Home")';
        });

        function getServerStatus() {
            $.ajax({
                url: '@Url.Action("GetServerStatus")',
                type: 'post',
                dataType: 'json',
                success: function(result) {
                    if (result && result.success && result.expandos) {
                        $('#uploadFlow').text('上传 ' + result.expandos.uploadFlow + ' KB');
                        $('#downloadFlow').text('下载 ' + result.expandos.downloadFlow + ' KB');
                        $('#usedStorge').text(result.expandos.usedStorge + ' M');
                        $('#totalStorge').text(result.totalStorge + ' M');
                    }
                    setTimeout(getServerStatus, 10000);
                }
            });
        }
    </script>
}
@section css
{
    <style type="text/css">
        #tbServerStatus {
            width: 68%;
            margin: 10px auto;
            border-spacing:0;
            border-collapse:collapse;
        }
        #tbServerStatus thead tr,#tbServerStatus tbody tr {
            height: 45px;
        }
        #tbServerStatus tbody tr td {
            border: 1px solid gray;
            text-indent: 20px;
        }
        #errorContain {
            text-align: center;
            margin: 20px;
        }
    </style>
}
<div class="validation-summary-errors" id="errorContain">
    @TempData[PresetValues.TempDataKeys.ErrorMessage]
</div>
<table id="tbServerStatus">
    <thead>
        <tr>
            <th colspan="2">
                @ViewBag.Title
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">
                @Html.LabelFor(m => m.Case)
                @Html.DisplayFor(m => m.Case)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                @Html.LabelFor(m => m.Task)
                @Html.DisplayFor(m => m.Task)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.TaskStatus)
                @Html.DisplayFor(m => m.TaskStatus)
            </td>
            <td align="center" style="border-left: 0;">
                <input id="btnViewTask" type="button" value="查看会话" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                @Html.LabelFor(m => m.Keywords)
                @if (Model != null && Model.Keywords != null)
                {
                    StringBuilder sbKeyword = new StringBuilder();
                    int i = 0;
                    foreach (var keyword in Model.Keywords)
                    {
                        string keywordHtml = string.Format(keyword.IsWarning == true ?
                                                               "<span style='color:red;'>{0}</span>" :
                                                               "<span>{0}</span>",
                                                           keyword.KeywordName);
                        sbKeyword.Append(keywordHtml + (++i == Model.Keywords.Count ? string.Empty : "\\"));
                    }
                    @Html.Raw(sbKeyword.ToString())
                }
            </td>
        </tr>
        <tr>
            <td colspan="2">
                @Html.LabelFor(m => m.LineDesc)
                @Html.DisplayFor(m => m.LineDesc)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                @Html.LabelFor(m => m.Protocols)
                @Html.DisplayFor(m => m.Protocols)
            </td>
        </tr>
        @if (Model != null && Model.IpSetting != null)
        {
            <tr>
                <td colspan="2">
                    @Html.LabelFor(m => m.IpSetting)
                    @string.Format(Resources.ViewResource.Home_Index_ServerFormat, 
                                   Model.IpSetting.Ip, Model.IpSetting.SubnetMask, Model.IpSetting.Gateway)
                </td>
            </tr>
        }
        <tr>
            <td colspan="2">
                @Html.LabelFor(m => m.MonitorInfo)
                @if (Model != null && Model.MonitorInfo != null)
                {
                    string monitorInfoHtml = string.Empty;
                    foreach (var monitorKeypair in Model.MonitorInfo)
                    {
                        monitorInfoHtml += string.Format("{0} ({1})/", monitorKeypair.Key, monitorKeypair.Value);
                    }
                    @Html.Raw(string.IsNullOrWhiteSpace(monitorInfoHtml) ? 
                                  monitorInfoHtml : monitorInfoHtml.Remove(monitorInfoHtml.Length - 1))
                }
            </td>
        </tr>
        @if (Model != null && Model.ServerStatus != null)
        {
            <tr>
                <td colspan="2">
                    <label>@Resources.ViewResource.Home_Index_FlowStatus</label>
                    @Html.Raw(
                        string.Format(Resources.ViewResource.Home_Index_FlowFormat, 
                                      Model.ServerStatus.UploadFlow, Model.ServerStatus.DownloadFlow))
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <label>@Resources.ViewResource.Home_Index_StorgeStatus</label>
                    @Html.Raw(string.Format(Resources.ViewResource.Home_Index_StorgeFormat, 
                                            Model.ServerStatus.UsedStorge, Model.ServerStatus.TotalStorge))
                </td>
            </tr>
        }
    </tbody>
</table>
